#coding=utf-8
import time  
from selenium import webdriver  
import re

#driver = webdriver.Chrome(r'F:\Python\Economic\chromedriver_win32\chromedriver.exe')  #打开浏览器

def batch_check_bonds_rate(driver):
        
    elem = driver.get('https://cn.investing.com/rates-bonds/world-government-bonds#rates_bonds_table_43')  #打开网址
    information = "" #把结果要保存的变量先初始化好
    print('Bonds rates are:(ranked by GDP)\n')

    def get_info(country='rates_bonds_table_1"',country2='rates_bonds_table_59'):   #定义主要方法： 复制利率到information变量，并同时打印出来
        nonlocal information #调用父方法的变量
        status = driver.page_source #把全网页源代码文本保存在status变量里
        rate = status[status.find(country):status.find(country2)]
        filtered_service_log = re.findall("<td (.*?)>(.*?)<\/td>",rate) #把coding中间的部分去掉，只留下人类可读部分
        filtered_service_log1 = re.findall("-last(.*?)\)",str(filtered_service_log))
        filtered_service_log2 = re.findall("<a (.*?)>(.*?)<\/a>",str(filtered_service_log))
        #print('The latest rate is: \n')
        for number in range(len(filtered_service_log2)):
                print(filtered_service_log2[number][1],filtered_service_log1[number*2])
                information = information + filtered_service_log2[number][1] + ' '+ filtered_service_log1[number*2] + ' '
        #print('\n')  #前后空好行

    information = information + '\nBonds rates are:(ranked by GDP)'
    
    get_info('rates_bonds_table_1"','rates_bonds_table_59')
    print('\n')
    get_info('rates_bonds_table_54"','rates_bonds_table_70')
    print('\n')
    get_info('rates_bonds_table_4"','rates_bonds_table_6"')
    print('\n')
    get_info('rates_bonds_table_20"','rates_bonds_table_42')
    print('\n')
    get_info('rates_bonds_table_3"','rates_bonds_table_7"')


    #print(information)  #根据需要打印出利率详情统计。

    #driver.quit()
    

#batch_check_bonds_rate()


